package dao;

import util.PinyinUtil;

import java.io.File;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Formatter;
import java.util.Objects;
/**
 * @author: cheng
 * @date: 2023/7/4 19:32
 * @description: file_meta实体类
 */

public class FileMeta {
    private int id;
    private String name;
    private String path;
    private boolean isDirectory;
    private long size;
    private long lastModified;

    public String getPinyin(){
        //全拼
        return PinyinUtil.get(name,true);
    }

    public String getPinyinFirst(){
        //首字母
        return PinyinUtil.get(name,false);
    }

    public String getSizeText(){
        //将size值进行合理单位换算,使其更加易读
        //单位有Byte KB MB GB
        //size < 1024 使用Byte,大于1024 小于1024 * 1024则使用MB
        double curSize = size;//因为时long,换下
        String[] utils ={"Byte","KB","MB","GB"};

        for(int level = 0;level < utils.length;level++){
            if(curSize < 1024){
                return String.format("%.2f " + utils[level],new BigDecimal(curSize));
            }
            curSize /= 1024;
        }
        return String.format("%.2f GB",new BigDecimal(curSize));
    }

    public String getLastModifiedText(){
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(lastModified);
    }

    public FileMeta(String name, String path, boolean isDirectory, long size, long lastModified) {
        this.name = name;
        this.path = path;
        this.isDirectory = isDirectory;
        this.size = size;
        this.lastModified = lastModified;
    }

    public FileMeta(File file){
        this(file.getName(),file.getParent(),file.isDirectory(),file.length(),file.lastModified());
    }

    @Override
    public boolean equals(Object o){
        if(this == o){
            return true;//自己和自己比较
        }
        if (o == null){
            return false;//是否为空
        }
        if(getClass() != o.getClass()){
            return false;//比较类型
        }

        FileMeta fileMeta = (FileMeta)o;
        return name.equals(fileMeta.name) && path.equals(fileMeta.path) && isDirectory == fileMeta.isDirectory;

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public boolean isDirectory() {
        return isDirectory;
    }

    public void setDirectory(boolean directory) {
        isDirectory = directory;
    }

    public long getSize() {
        return size;
    }

    public void setSize(long size) {
        this.size = size;
    }

    public long getLastModified() {
        return lastModified;
    }

    public void setLastModified(long lastModified) {
        this.lastModified = lastModified;
    }
}
